home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: in2.uu.net!shore!mv!usenet
- From: ENGR@GSSI.MV.COM (Michael Furman)
- Subject: Re: Advanced C++ question...
- Message-ID: <DovzxE.B0E@mv.mv.com>
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- Organization: GSSI
- Date: Tue, 26 Mar 1996 18:12:50 GMT
- References: <4iprfg$1ui@aadt> <DoMH88.5wv@mv.mv.com> <4j0u62$6na@newshost.centrum.is>
- X-Newsreader: WinVN 0.99.7
- X-Nntp-Posting-Host: gssi.mv.com
-
- In article <4j0u62$6na@newshost.centrum.is>, bjarnir@centrum.is says...
- >
- >In article <DoMH88.5wv@mv.mv.com>, ENGR@GSSI.MV.COM (Michael Furman) says:
- >>
- >>In article <4iprfg$1ui@aadt>, david_hooker@sdt.com says...
- >>>
- >>>Hi all...
- >>>
- >>>I want to write an operator* for a class, but I want to do
- >>>one of 2 things:
- >>> 1. Call one of two different operator*'s depending on whether
- >>> it is an lvalue or rvalue
- >>> OR
- >>> 2. Somehow determine in the function if it is being used as an
- >>> lvalue or an rvalue.
- >>> [......]
- >>>The reason for this is that this class accesses data differently
- >>depending
- >>>on if it is a read or write access.
- >>
- >>It is impossible with "*" operator. But you can define conversion
- >>functions
- >>for your class - it will allow exactly what you need.
- >>
- >
- >Could you explain this a bit further. I have a similar problem to solve.
-
- You can define a special class for using instead if embedded type (pseude_int
- instead of int in my example below). Define conversion to value (int) for
- reading it and "operator =" for writing:
-
-
- #include <stdio.h>
-
- class pseudo_int
- {
- public:
- operator int()
- {
- printf("Value was read\n");
- return 0; // Or get / calculate any value and return it
- }
- int operator = (const int & x)
- {
- printf("Value was assigned: %d\n", x);
- // Do whatever you need with value x
- return x;
- }
- // Additional members / member functions if needed
- } xxx;
-
- int main()
- {
- int i;
- i = xxx;
- xxx = 5;
- return 0;
- }
-
-
- --
- <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
- ---------------------------------------------------------------
- Michael Furman, (603)893-1109
- Geophysical Survey Systems, Inc. fax:(603)889-3984
- 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- North Salem, NH 03073-0097 71543.1334@compuserve.com
- ---------------------------------------------------------------
-
-